Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed exception on linear_quantize_activations for ImageType output #2385

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Zerui18
Copy link

@Zerui18 Zerui18 commented Nov 4, 2024

Issue

Calling cto.coreml.experimental.linear_quantize_activations on a model with ct.ImageType output(s) fails at the model output because check_intermediate_output expects output_value to be a np.ndarray but the model's final output is PIL.Image.

Solution

This patch adds a simple attempt to convert the output_value to np.ndarray before the computations.

output_value may be a PIL image, in which case convert it to numpy array so that the following calculations can still be run
@TobyRoseman
Copy link
Collaborator

Thanks for the fix. Can you add a unit test that fails without this fix, but passes with this fix?

@Zerui18
Copy link
Author

Zerui18 commented Nov 5, 2024

Hi @TobyRoseman I've just discovered that the root cause of this issue is the function ModelDebugger.step which applies check_intermediate_output explicitly to all original model outputs as seen here:

for (output_name, output_value) in model_outputs.items():
operation = self.block_info.operations.get(output_name, None)
if not step_fn(output_value, output_name, operation, activation_stats_dict):
return

Hence, I believe rather than changing check_intermediate_output which is correctly expecting only np.ndarray, it's better to adjust ModelDebugger.step so that it doesn't call check_intermediate_output on the final model outputs. More concretely I'm thinking that we can add a new argument intermediate_outputs_only=False which can be set to True to skip the above block of code, whilst retaining the original functionality of step by default.

What do you think?

@TobyRoseman
Copy link
Collaborator

Thanks @Zerui18 for looking into this. I'm not very familiar with that part of the code. I think @yixingli-apple or @junpeiz should be able to help here.

@yixingli-apple yixingli-apple self-requested a review November 6, 2024 01:51
@Zerui18
Copy link
Author

Zerui18 commented Nov 12, 2024

Hi, is there any updates on this?

@yixingli-apple
Copy link
Collaborator

Hi @Zerui18 ,

I agree with you we should make a fix in _model_debugger.py instead.
When we're doing calibration, we should exclude any output tensors. There’s no need to add a new argument intermediate_outputs_only=False, but simply exclude all output tensors (in intermediate_outputs) should be good.

We just released coremltools 8.1, and the part of code have been changed. Can you please rebase and create a PR based on that?

@Zerui18
Copy link
Author

Zerui18 commented Nov 21, 2024

Hi @yixingli-apple , I've created a new PR #2405 based on your suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants